package com.cy.jxc.controller;

import com.cy.jxc.entity.User;
import com.cy.jxc.service.LoginService;
import com.cy.jxc.vo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {
    @Autowired
    LoginService loginService;
    @RequestMapping("/loginbyname")
    public JsonResult findUserByname(@RequestParam String username){
        System.out.println(username);
        User userByname = loginService.findUserByname(username);
        System.out.println(userByname);
        if (userByname!=null){
            return new JsonResult(userByname);
        }
        return new JsonResult();
    }
    @PostMapping("/login")
    public JsonResult login(@Validated User user, BindingResult result, boolean remember, HttpServletRequest request){
        System.out.println(remember);
//        list存储错误信息
        List<String> errorlist=new ArrayList<>();
        if (result.hasErrors()){
            List<ObjectError> allErrors = result.getAllErrors();
            allErrors.forEach((error)->{
                errorlist.add(error.getDefaultMessage());
            });
            return new JsonResult(errorlist);
        }else {


        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(user.getUsername(),user.getPassword(),remember);

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
            System.out.println("登录成功");
//          在shiro中保存登录用户的所有信息到session域中
//            HttpSession session = request.getSession();
//            session.setAttribute("nowusername", user.getUsername());
           return new JsonResult("登录成功");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误");
            return new JsonResult(e);
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("密码错误");
            return new JsonResult(e);
        }
//        return new JsonResult("登录失败");
        }
    }
  /*  @RequestMapping("/logout")
    public  String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "logout";
    }*/
}
